<?php

require_once('./MysqlService.php');

class BaseDAO {
 
	//var $connectionHandle;
	var $VO;
	var $VOInstance;
	var $mysqlObj;
	

	function BaseDAO()
	{
		$this->mysqlObj = MysqlService();
		$GLOBALS['amfphp']['encoding'] = 'amf3';
	}
	 
	function _getRecords($mysqlQueryString) {
 
		$result = $this->_execute($mysqlQueryString);
		while ($row = $result->fetch_assoc()){	
			$arrayOfRecords[]=$this->convertToVO($row);
		}
		return( $arrayOfRecords );
 
	}
	function convertToVO($row)
	{
		/* Create the object */
		$class = $this->VO;
  		$obj = new $this->VO();
  		/* Explode the array and set the objects's instance data */
		foreach ($row as $key => $value){
			if (strpos($key, 'photodata') !== false) 
			{
				$value = new ByteArray($value);
			}
			$obj->{$key} = $value;
  		}
		return $obj;
	}
	function _execute($sql)
	{
		$result = $this->mysqlObj->query($sql);
		$this->_checkErrors($sql);
		return $result;
	}
	function _checkErrors($sql)
	{
		$err = $this->mysqlObj->error;
		if($err)
		{
			$message = "The command '".$sql."' caused the following mysql error: ".$err.".";
			trigger_error($message, E_USER_ERROR);
			exit;
		}
		else
		{
			return;
		}
	}
}

?>